package cn.iocoder.yudao.module.tms.controller.admin.orderfollowlog;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.tms.controller.admin.orderfollowlog.vo.*;
import cn.iocoder.yudao.module.tms.dal.dataobject.orderfollowlog.OrderFollowLogDO;
import cn.iocoder.yudao.module.tms.dal.dataobject.orderfollowlog.OrderFollowLogAttachmentDO;
import cn.iocoder.yudao.module.tms.service.orderfollowlog.OrderFollowLogService;
import cn.iocoder.yudao.module.tms.service.orderfollowlog.OrderFollowLogAttachmentService;

@Tag(name = "管理后台 - 订单跟进日志")
@RestController
@RequestMapping("/tms/product-follow-log")
@Validated
public class OrderFollowLogController {

    @Resource
    private OrderFollowLogService orderFollowLogService;
    
    @Resource
    private OrderFollowLogAttachmentService orderFollowLogAttachmentService;

    @PostMapping("/create")
    @Operation(summary = "创建订单跟进日志")
    @PreAuthorize("@ss.hasPermission('tms:product-follow-log:create')")
    public CommonResult<Long> createOrderFollowLog(@Valid @RequestBody OrderFollowLogSaveReqVO createReqVO) {
        return success(orderFollowLogService.createOrderFollowLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单跟进日志")
    @PreAuthorize("@ss.hasPermission('tms:product-follow-log:update')")
    public CommonResult<Boolean> updateOrderFollowLog(@Valid @RequestBody OrderFollowLogSaveReqVO updateReqVO) {
        orderFollowLogService.updateOrderFollowLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单跟进日志")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tms:product-follow-log:delete')")
    public CommonResult<Boolean> deleteOrderFollowLog(@RequestParam("id") Long id) {
        orderFollowLogService.deleteOrderFollowLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除订单跟进日志")
                @PreAuthorize("@ss.hasPermission('tms:product-follow-log:delete')")
    public CommonResult<Boolean> deleteOrderFollowLogList(@RequestParam("ids") List<Long> ids) {
        orderFollowLogService.deleteOrderFollowLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单跟进日志")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tms:product-follow-log:query')")
    public CommonResult<OrderFollowLogRespVO> getOrderFollowLog(@RequestParam("id") Long id) {
        OrderFollowLogDO orderFollowLog = orderFollowLogService.getOrderFollowLog(id);
        OrderFollowLogRespVO respVO = BeanUtils.toBean(orderFollowLog, OrderFollowLogRespVO.class);
        // 设置附件列表
        List<OrderFollowLogAttachmentDO> attachments = orderFollowLogAttachmentService.getAttachmentsByFollowLogId(id);
        respVO.setAttachments(BeanUtils.toBean(attachments, OrderFollowLogRespVO.FollowLogAttachment.class));
        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单跟进日志分页")
    @PreAuthorize("@ss.hasPermission('tms:product-follow-log:query')")
    public CommonResult<PageResult<OrderFollowLogRespVO>> getOrderFollowLogPage(@Valid OrderFollowLogPageReqVO pageReqVO) {
        PageResult<OrderFollowLogDO> pageResult = orderFollowLogService.getOrderFollowLogPage(pageReqVO);
        PageResult<OrderFollowLogRespVO> respPageResult = BeanUtils.toBean(pageResult, OrderFollowLogRespVO.class);
        // 设置附件列表
        List<OrderFollowLogRespVO> list = respPageResult.getList();
        for (OrderFollowLogRespVO respVO : list) {
            List<OrderFollowLogAttachmentDO> attachments = orderFollowLogAttachmentService.getAttachmentsByFollowLogId(respVO.getId());
            respVO.setAttachments(BeanUtils.toBean(attachments, OrderFollowLogRespVO.FollowLogAttachment.class));
        }
        return success(respPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单跟进日志 Excel")
    @PreAuthorize("@ss.hasPermission('tms:product-follow-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOrderFollowLogExcel(@Valid OrderFollowLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OrderFollowLogDO> list = orderFollowLogService.getOrderFollowLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单跟进日志.xls", "数据", OrderFollowLogRespVO.class,
                        BeanUtils.toBean(list, OrderFollowLogRespVO.class));
    }

}